/** 发牌动画 */
class SendCard{
    /** 牌的缩放比例 */
    private scaleValue:number;
    /** 中心牌 */
    private centerImg:eui.Image;
    /** 飞行的牌 */
    private flyImg:eui.Image;
    /** 一个玩家是牌飞行时间 */
    private flyTime:number;
    /** 飞的牌数量 */
    private flyNum:number;
    /** 结束位置的牌 */
    private endImg:Array<eui.Image>;
    /** 结束函数 */
    private endFun:Function;

    public constructor(flytm:number, rfun:Function){
        if(!D.App.gameView) return;
        this.endFun = rfun;
        this.centerImg = new eui.Image();
        this.flyImg = new eui.Image();
        this.flyImg.source = this.centerImg.source = "card_0_png";
        this.flyImg.x = this.centerImg.x = D.App.gameView.width / 2 - this.flyImg.width / 2;
        this.flyImg.y = this.centerImg.y = D.App.gameView.height / 2 - this.flyImg.height / 2;
        this.endImg = [];
        this.flyTime = flytm;
        this.flyNum = 5;
        this.scaleValue = 0.8;
        this.flyImg.scaleX = this.flyImg.scaleY = this.scaleValue;
        this.centerImg.scaleX = this.centerImg.scaleY = this.scaleValue;
    }

    /** 添加结束点 */
    public addEndPoint(pt:Array<number>, rfun):void{
        let tmp:eui.Image = new eui.Image();
        tmp.source = "card_0_png";
        tmp.scaleX = tmp.scaleY = this.scaleValue;
        tmp.x = pt[0];
        tmp.y = pt[1];
        tmp.visible = false;
        D.App.gameView.addChild(tmp);
        this.endImg.push(tmp);
    }

    /** 开始发牌 */
    public play():void{
        D.App.gameView.addChild(this.centerImg);
        D.App.gameView.addChild(this.flyImg);
        let ss:number = this.flyTime / this.endImg.length;
        for(let i=0;i<this.flyNum;i++){
            for(let m=0;m<this.endImg.length;m++){
                egret.Tween.get(this.flyImg).wait(i*this.flyTime+ss*m).to({x:this.endImg[m].x,y:this.endImg[m].y}, ss).set({x:this.centerImg.x,y:this.centerImg.y});
            }
        }
        for(let m=0;m<this.endImg.length;m++){
            egret.Tween.get(this.endImg[m]).wait(ss*(m+1)).to({visible:true}, ss);
        }
        egret.Tween.get(this.flyImg).wait(this.flyNum*this.flyTime+100).call(this.EndFly, this);
    }

    /** 结束牌飞行 */
    private EndFly():void{
        console.log("EndFly");
        if(!D.App.gameView) return;
        D.App.gameView.removeChild(this.centerImg);
        console.log(this.centerImg.parent);
        D.App.gameView.removeChild(this.flyImg);
        for(let k in this.endImg){
            D.App.gameView.removeChild(this.endImg[k]);
        }
        //egret.Tween.removeAllTweens();
        this.endFun && this.endFun();
    }
}